package com.aofeng.hybridwork.android.task;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import com.aofeng.hybrid.activity.HybridActivity;
import com.aofeng.hybrid.service.EntityService;
import com.aofeng.hybrid.service.SqlService;
import com.aofeng.hybridwork.application.HybridApplication;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 下载安检计划异步任务
 * Created by LGY on 2016-08-25.
 */
public class DownloadAsyncTask extends AsyncTask<String, String, Boolean> {
    HybridActivity hybridActivity;
    //计划在客户端的序号，回调用
    String index;

    JSONObject userinfo;
    JSONObject equipment;
    JSONObject hiddentrouble;
    JSONObject customer;

    public DownloadAsyncTask(HybridActivity hybridActivity) {
        this.hybridActivity = hybridActivity;
    }
    @Override
    /**
     * params:
     * 下载的url
     */
    protected Boolean doInBackground(String... params) {
        try {
            index = "1";
            //拿到的数据
            userinfo = new JSONObject(params[0]);
            equipment = new JSONObject(params[1]);
            hiddentrouble = new JSONObject(params[2]);
            customer = new JSONObject(params[3]);

            //fuck for the illusion
            //删除hand plan表内相同册号的数据
            SqlService ss = new SqlService(hybridActivity);
            String f_userid = userinfo.getString("f_userid");
            ss.run("delete from t_check_plan where f_userid='" + f_userid + "'");
            ss.run("delete from t_aboutuser where f_userid='" + f_userid + "'");
            ss.run("delete from t_equipmentinspection where f_userid='" + f_userid + "'");
            ss.run("delete from t_hinvestigation where f_userid='" + f_userid + "'");

            //保存出错数据
            EntityService es = new EntityService(hybridActivity);

            publishProgress(new String[]{ 35 + "", "保存出错数据......"});
            userinfo.put("f_upload","未上传");
            equipment.put("f_userid",f_userid);
            hiddentrouble.put("f_userid",f_userid);
            customer.put("f_userid",f_userid);
            es.append("t_check_plan", userinfo);
            es.append("t_aboutuser", equipment);
            es.append("t_equipmentinspection", hiddentrouble);
            es.append("t_hinvestigation", customer);
            publishProgress(new String[]{ 100 + "", "保存完成。"});
            return true;
        } catch(Exception e) {
            return false;
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        hybridActivity.createProgressDialog(ProgressDialog.STYLE_SPINNER);
    }

    @Override
    protected void onPostExecute(Boolean flag) {
        super.onPostExecute(flag);
        hybridActivity.closeProgress();
        hybridActivity.mWebView.loadUrl("javascript:HostApp.__callback__(" + flag +");");
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        int progress = Integer.parseInt(values[0]);
        hybridActivity.setProgress(progress, values[1]);
    }
}
